﻿/*
 * =====================================================================================
 *
 *       Filename:  FIleHelper.cs
 *
 *    Description:  文件操作工具封装类
 *
 *        Version:  1.0
 *        Created:  
 *       Revision:  none
 *       Compiler:  
 *
 *         Author:   imbedinlove
 *        Company:  
 *       EditTime:  2014年6月20日 11:12:11
 *
 * =====================================================================================

 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace ImbedLibrary
{
    public class FileHelper
    {
        /// <summary>
        /// 获取文件夹内文件信息
        /// </summary>
        /// <param name="folder">文件夹路径</param>
        /// <returns></returns>
        public static IList<FileInfo> GetFileInfo(string folder)
        {
            IList<FileInfo> fileInfos = new List<FileInfo>();
            foreach (var item in Directory.GetFiles(folder))
            {
                fileInfos.Add(new FileInfo(item));
            }
            return fileInfos;
        }

        /// <summary>
        /// 创建隐藏文件
        /// </summary>
        /// <param name="file"></param>
        public static void CreateHiddenFile(string file)
        {
            if (File.Exists(file))
            {
                //File.Delete(file);
                return;
            }
            using(FileStream fs = File.Create(file))
            {
                File.SetAttributes(file, FileAttributes.Hidden);
            }
        }

        /// <summary>
        /// 返回文件的Hash码
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string HashCode(string filePath ) 
        {
            using (var stream = File.OpenRead(filePath)  )
            {
                var md5 = MD5.Create();
                var code = md5.ComputeHash(stream);
                var codeStr = Convert.ToBase64String(code);
                return codeStr;
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        public static void Delete(string path)
        {
            File.Delete(path);
        }

        /// <summary>
        /// 获取文件夹内文件信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IEnumerable<FileInfo> GetFiles(string path, SearchOption so = SearchOption.TopDirectoryOnly)
        {
            if (!Directory.Exists(path))
                throw new DirectoryNotFoundException();
            string[] fileNames = null;
            List<FileInfo> files = new List<FileInfo>();

            fileNames = Directory.GetFiles(path, "*",so);
            foreach (string name in fileNames)
            {
                files.Add(new FileInfo(name));
            }
            return files;
        }


        /*
         * 重新封装
         * 2014年9月3日 14:42:13
         * 以下内容
         */

        public FileHelper(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new PathIsNullException();
            }
            this.path = path;
        }

        public string path { get; set; }

        /// <summary>
        /// 返回FileStream
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="access"></param>
        /// <param name="share"></param>
        /// <returns></returns>
        public FileStream GetFS(FileMode mode, FileAccess access, FileShare share)
        {
            return new FileStream(path, mode, access, share);
        }

        /// <summary>
        /// 返回StreamWriter
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public StreamWriter GetSW(Encoding encoding,FileMode mode = FileMode.Append)
        {
            return new StreamWriter(GetFS(mode, FileAccess.Write, FileShare.ReadWrite), encoding);
        }

        public StreamReader GetSR()
        {
            return new StreamReader(GetFS(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
        }

        public StreamReader GetSR(Encoding encoding)
        {
            return new StreamReader(GetFS(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding);
        }


        /// <summary>
        /// 替换行内容
        /// </summary>
        /// <param name="newLine">新行内容</param>
        /// <param name="lineCount">行号</param>
        public void Replace( string newLine,int lineCount,Encoding encoding)
        {
            //行号和行内容
            // 行号从1开始
            Dictionary<int, string> contents = new Dictionary<int, string>();
            string content = string.Empty;
            int count = 1;
            using (StreamReader sr = GetSR(encoding))
            {
                try
                {
                    while (!sr.EndOfStream)
                    {
                        content = sr.ReadLine();
                        contents.Add(count, content);
                        count++;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            contents[lineCount] = newLine;
            using (StreamWriter sw = GetSW(encoding,FileMode.Open))
            {
                foreach (var item in contents)
                {
                    sw.WriteLine(item.Value);
                }
            }
        }

    }
}
